﻿//简易公用AJAX请求
function AJAXFunction() {
    var _this = this;
    //请求参数
    this.config = arguments[0] || {};
    this.type = this.config.type || 'POST';
    //this.type = this.config.type || 'GET';
    this.url = this.config.url || '';
    this.contentType = this.config.contentType || 'application/json; charset=utf-8';
    this.dataType = this.config.dataType || 'json';
    this.async = typeof (this.config.async) == 'boolean' ? this.config.async : true;
    this.param = this.config.param || {};
    this.data = this.config.data || $.JSON.encode(this.param);

    //请求发送
    this.SendFun = function () {
        $.ajax({
            type: this.type,
            url: this.url,
            contentType: this.contentType,
            dataType: this.dataType,
            data: this.data,
            async: this.async,
            complete: function (xmlObj, Status) { _this.CpFun(xmlObj, Status); },
            error: function (xmlObj, Status) {
                if (Status == 'parsererror') {
                    //alert('请求或者返回数据异常');
                    return;
                }
                if (typeof (_this.FaFun) != 'function') {
                    //alert('请求或者返回数据异常');
                } else {
                    _this.FaFun(xmlObj, Status);
                }
            },
            success: function (data) {
                if (data.Status >= 1) {
                    _this.SuFun(data);
                }
                else {
                    if (typeof (_this.SddsFun) != 'function') {
                        //alert(data.Message);
                    } else {
                        _this.SddsFun(data);
                    }
                }
            }
        });
    };
    //请求失败和成功
    this.SuFun = function () { };
    this.FaFun = null;
    this.CpFun = function () { };
    this.SddsFun = null;
}

//Browsers
var Browser = { 'UA': navigator.userAgent.toLowerCase() };
Browser.IE6 = Browser.UA.match(/msie ([\d.]+)/) && Browser.UA.match(/msie ([\d.]+)/)[1] == '6.0';
Browser.IE7 = Browser.UA.match(/msie ([\d.]+)/) && Browser.UA.match(/msie ([\d.]+)/)[1] == '7.0';
Browser.IE8 = Browser.UA.match(/msie ([\d.]+)/) && Browser.UA.match(/msie ([\d.]+)/)[1] == '8.0';
Browser.IE9 = Browser.UA.match(/msie ([\d.]+)/) && Browser.UA.match(/msie ([\d.]+)/)[1] == '9.0';
Browser.IE10 = Browser.UA.match(/msie ([\d.]+)/) && Browser.UA.match(/msie ([\d.]+)/)[1] == '10.0';
Browser.IE11 = Browser.UA.match(/msie ([\d.]+)/) && Browser.UA.match(/msie ([\d.]+)/)[1] == '11.0';
Browser.Firfox = Browser.UA.match(/firefox\/([\d.]+)/);
Browser.Chrome = Browser.UA.match(/chrome\/([\d.]+)/);
Browser.Opera = Browser.UA.match(/opera.([\d.]+)/);
Browser.Safari = Browser.UA.match(/version\/([\d.]+).*safari/);


//Cookie
var Cookie = {};
Cookie.Set = function (cookieName, cookieValue) {
    var args = arguments;
    var lens = args.length;
    var expires = lens > 2 ? (/^([\-]?)[0-9]+$/.test(args[2]) ? parseInt(args[2]) : 365) : 365;
    var path = lens > 3 ? args[3] : '/';
    var domain = lens > 4 ? args[4] : null;
    var secure = lens > 5 ? args[5] : null;
    var cook = [];
    cook.push(cookieName + '=' + encodeURIComponent(cookieValue));
    cook.push('expires=' + new Date(new Date().getTime() + expires * 86400).toGMTString());
    cook.push('path=' + path);
    if (domain) cook.push('domain=' + domain);
    if (secure) cook.push('secure');
    document.cookie = cook.join(';');
};
Cookie.Get = function (cookieName) {
    var getArgs = cookieName + '=';
    var cook = document.cookie;
    var offset = cook.indexOf(getArgs);
    if (offset >= 0) {
        offset += getArgs.length;
        var end = cook.indexOf(';', offset);
        if (end < 0) end = cook.length;
        return decodeURIComponent(cook.substring(offset, end));
    }
    return null;
};
Cookie.Clear = function (cookieName) {
    if (Cookie.Get(cookieName))
        Cookie.Set(name, '', -10);
};


//String Prototype
String.prototype.Trim = function () {
    return (this || '').replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, '');
};
String.prototype.StartWith = function (s) {
    var org = this || '';
    if (org.length >= s.length && org.indexOf(s) == 0)
        return true;
    return false;
};
String.prototype.EndWith = function (s) {
    var org = this || '';
    if (org.length >= s.length && org.lastIndexOf(s) == org.length - s.length)
        return true;
    return false;
};
String.prototype.TrimStart = function (s) {
    var org = this || '';
    if (org.StartWith(s))
        return org.substring(s.length);
    return org;
};
String.prototype.TrimEnd = function (s) {
    var org = this || '';
    if (org.EndWith(s))
        return org.substring(0, org.length - s.length);
    return org;
};
String.prototype.IsDate = function () {
    var org = (this || '').replace(/-/g, '/');
    var regex = /^(\d{4})\/(\d{1,2})\/(\d{1,2})?$/;
    if (regex.test(org)) {
        var _year = parseInt(RegExp.$1, 10),
                    _month = parseInt(RegExp.$2, 10) - 1,
                    _day = parseInt(RegExp.$3, 10);
        var newDate = new Date(_year, _month, _day);
        if (!(newDate.getFullYear() == _year && newDate.getMonth() == _month && newDate.getDate() == _day))
            return false;
        else
            return true;
    }
    return false;
};
String.prototype.IsDateTime = function () {
    var org = (this || '').replace(/-/g, '/');
    var regex = /^(\d{4})\/(\d{1,2})\/(\d{1,2})(\s(\d{1,2}):(\d{1,2}):(\d{1,2}))$/;
    if (regex.test(org)) {
        var _year = parseInt(RegExp.$1, 10),
                    _month = parseInt(RegExp.$2, 10) - 1,
                    _day = parseInt(RegExp.$3, 10),
                    _hour = parseInt(RegExp.$5, 10),
                    _minute = parseInt(RegExp.$6, 10),
                    _second = parseInt(RegExp.$7, 10);
        var newDate = new Date(_year, _month, _day, _hour, _minute, _second);
        if (!(newDate.getFullYear() == _year && newDate.getMonth() == _month
                      && newDate.getDate() == _day && newDate.getHours() == _hour
                      && newDate.getMinutes() == _minute && newDate.getSeconds() == _second))
            return false;
        else
            return true;
    }
    return false;
};
String.prototype.ConvertToDate = function () {
    var org = (this || '').replace(/-/g, '/');
    if (org.IsDate()) {
        (/^(\d{4})\/(\d{1,2})\/(\d{1,2})?$/).test(org);
        return new Date(parseInt(RegExp.$1, 10), parseInt(RegExp.$2, 10) - 1, parseInt(RegExp.$3, 10));
    }
    return new Date();
};
String.prototype.ConvertToDateTime = function () {
    var org = (this || '').replace(/-/g, '/');
    if (org.IsDateTime()) {
        (/^(\d{4})\/(\d{1,2})\/(\d{1,2})(\s(\d{1,2}):(\d{1,2}):(\d{1,2}))$/).test(org);
        return new Date(parseInt(RegExp.$1, 10),
                        parseInt(RegExp.$2, 10) - 1,
                        parseInt(RegExp.$3, 10),
                        parseInt(RegExp.$5, 10),
                        parseInt(RegExp.$6, 10),
                        parseInt(RegExp.$7, 10)
                       );
    }
    return new Date();
};


//date Prototype
Date.prototype.DateToJson = function () {
    return { 'Year': this.getFullYear(), 'Month': this.getMonth() + 1, 'Day': this.getDate() };
};
Date.prototype.DateTimeToJson = function () {
    return {
        'Year': this.getFullYear(),
        'Month': this.getMonth() + 1,
        'Day': this.getDate(),
        'Hour': this.getHours(),
        'Minute': this.getMinutes(),
        'Second': this.getSeconds()
    };
};
Date.prototype.DateDiff = function (date, timePart) {
    var unit = 1;
    switch (timePart) {
        case 'ms':
            unit = 1;
            break;
        case 's':
            unit = 1000;
            break;
        case 'm':
            unit = 60 * 1000;
            break;
        case 'h':
            unit = 60 * 60 * 1000;
            break;
        case 'd':
            unit = 24 * 60 * 60 * 1000;
            break;
    }
    var result = Math.floor((this.getTime() - date.getTime()) / unit);
    return result < 0 ? result + 1 : result;
};
Date.prototype.DateAdd = function (datePart, addValue) {
    var orgDate = this;
    switch (datePart.toLowerCase()) {
        case 'y':
            orgDate.setFullYear(orgDate.getFullYear() + addValue);
            break;
        case 'mm':
            orgDate.setMonth(orgDate.getMonth() + addValue);
            break;
        case 'd':
            orgDate.setDate(orgDate.getDate() + addValue);
            break;
        case 'h':
            orgDate.setHours(orgDate.getHours() + addValue);
            break;
        case 'm':
            orgDate.setMinutes(orgDate.getMinutes() + addValue);
            break;
        case 's':
            orgDate.setSeconds(orgDate.getSeconds() + addValue);
            break;
    }
    return orgDate;
};
Date.prototype.Format = function (format) {
    var date = this;
    var zeroize = function (value, length) {
        if (!length) {
            length = 2;
        }
        value = new String(value);
        for (var i = 0, zeros = ''; i < (length - value.length); i++) {
            zeros += '0';
        }
        return zeros + value;
    };
    var wk = ['日', '一', '二', '三', '四', '五', '六'];
    var month = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
    return format.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHms])\1?)\b/g, function ($0) {
        switch ($0) {
            case 'd':
                return date.getDate();
            case 'dd':
                return zeroize(date.getDate());
            case 'ddd':
                return wk[date.getDay()];
            case 'dddd':
                return '星期' + wk[date.getDay()];
            case 'M':
                return date.getMonth() + 1;
            case 'MM':
                return zeroize(date.getMonth() + 1);
            case 'MMM':
                return month[date.getMonth()];
            case 'yy':
                return new String(date.getFullYear()).substr(2);
            case 'yyyy':
                return date.getFullYear();
            case 'H':
                return date.getHours();
            case 'HH':
                return zeroize(date.getHours());
            case 'm':
                return date.getMinutes();
            case 'mm':
                return zeroize(date.getMinutes());
            case 's':
                return date.getSeconds();
            case 'ss':
                return zeroize(date.getSeconds());
        }
    });
};

//indexOf for IE8
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt) {
        var len = this.length >>> 0;

        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this && this[from] === elt)
                return from;
        }
        return -1;
    };
};

//Event
var cancelEvent = function (event) {
    if (event)
        event.stopPropagation();
    else
        window.event.cancelBubble = true;
};
var fixEvent = function (event) {
    return event ? event : window.event;
};

var GetTimeAPMName = function () {
    var now = new Date();
    var hour = now.DateTimeToJson().Hour;
    if (hour >= 22 && hour < 6)
        return '深夜';
    if (hour >= 6 && hour < 9)
        return '上午';
    if (hour >= 9 && hour < 12)
        return '中午';
    if (hour >= 12 && hour < 19)
        return '下午';
    return '晚上';
};